package com.miplus.ccrm.core.service.strategy;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.common.JPubUtils;
import com.miplus.ccrm.core.controller.vo.LabelResVo;
import com.miplus.ccrm.core.controller.vo.strategy.CpListEditVo;
import com.miplus.ccrm.core.controller.vo.strategy.CpListReqVo;
import com.miplus.ccrm.core.controller.vo.strategy.CpListResVo;
import com.miplus.ccrm.core.pojo.*;
import com.miplus.ccrm.core.mapper.CpCalllistMapper;
import com.miplus.ccrm.core.service.crd.FiTaskService;
import com.miplus.ccrm.core.utils.EnumFiChannel;
import com.miplus.ccrm.core.utils.EnumRuleStepAction;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* @author pc
* @description 针对表【cp_calllist(回访清单表)】的数据库操作Service实现
* @createDate 2024-03-21 16:08:15
*/
@Service
public class CpCalllistServiceImpl extends MPJBaseServiceImpl<CpCalllistMapper, CpCalllist>
    implements CpCalllistService{
    @Resource
    ObJobService obJobService;
    @Resource
    ObJobAgentsService obJobAgentsService;
    @Override
    public IPage<CpListResVo> campaignList(CpListReqVo paras)
    {
        return selectJoinListPage(new Page<>(paras.getPageNum(), paras.getPageSize()),
                CpListResVo.class,
                new MPJLambdaWrapper<CpCalllist>()
                        .leftJoin(MsgDefine.class, MsgDefine::getId,CpCalllist::getSendmodel)
                        .selectAs("fun_getnames('CAMPAIGN.OBTAINTYPE',t.obtaintype)",CpListResVo::getObtaintname)
                        .selectAs("fun_getnames('CAMPAIGN.SCREENTYPE',t.apptype)",CpListResVo::getAppname)
                        .selectAs("fun_getnames('CAMPAIGN.CHANNEL',t.channel)",CpListResVo::getChannelname)
                        .selectAs("fun_getnames('CON.RELATION',t.relational)",CpListResVo::getRelationaldesc)
                        .selectAs("(select w.name from wf_workflow w where w.wfid=t.wfid)",CpListResVo::getWfname)
                        .selectAll(CpCalllist.class)
                        .selectAs(CpCalllist::getSendmodel,CpListResVo::getAimodel)
                        .eq(paras.getChannel()!=null,CpCalllist::getChannel,paras.getChannel())
                        .eq(JPubUtils.IsNotEmpty(paras.getClid()),CpCalllist::getClid,paras.getClid())
                        .eq(paras.getState()!=null,CpCalllist::getState,paras.getState())
                        .like(JPubUtils.IsNotEmpty(paras.getName()),CpCalllist::getName,paras.getName())
                        .orderByAsc(CpCalllist::getClid)
        ).convert(x->{
            if( x.getChannel() == EnumFiChannel.Pd.value){
                x.setObJob( obJobService.getById(x.getClid()));
                x.setObJobAgentsList( obJobAgentsService.getBaseMapper().selectList(
                        new QueryWrapper<ObJobAgents>().eq("jobid", x.getClid())
                ));
            }
             return x;
        });
    }

    @Override
    @Transactional
    public CmdResVo campaignUpdate(String $usrid, CpListEditVo paras)
    {
        CpCalllist def=new CpCalllist();
        BeanUtils.copyProperties(paras,def);
        if(JPubUtils.IsEmpty(paras.getWfid())){
            def.setWfid(null);
        }
        if (JPubUtils.IsEmpty(paras.getTriggerid())) {
            def.setTriggerid(null);
        }
        if(StringUtils.isNotEmpty(paras.getAimodel()))
        {
            def.setSendmodel(paras.getAimodel());
        }
        def.setMddt(new Date());
        def.setMdusr($usrid);
        int ret=baseMapper.updateById(def);
        if( def.getChannel() == EnumFiChannel.Pd.value)
        {
            ObJob obJob = new ObJob();
            BeanUtils.copyProperties(paras.getObJob(),obJob);
            obJob.setStarttime(paras.getStarttime());
            obJob.setEndtime(paras.getEndtime());
            obJob.setJobid(def.getClid());
            obJobService.updateById(obJob);
            obJobAgentsService.remove(new QueryWrapper<ObJobAgents>().eq("jobid",paras.getClid()));
            for(ObJobAgents x : paras.getObJobAgentsList())
            {
                x.setJobid(def.getClid());
                obJobAgentsService.save(x);
            }
        }
        return new CmdResVo(ret,def.getClid());
    }

    @Override
    public CmdResVo campaignInsert(String $usrid,CpListEditVo paras)
    {
        CpCalllist def=new CpCalllist();
        BeanUtils.copyProperties(paras,def);
        if(StringUtils.isNotEmpty(paras.getAimodel()))
        {
            def.setSendmodel(paras.getAimodel());
        }
        def.setCrdt(new Date());
        def.setCrusr($usrid);
        def.setMddt(new Date());
        def.setMdusr($usrid);
        int ret=baseMapper.insert(def);
        if( def.getChannel() == EnumFiChannel.Pd.value)
        {
            ObJob obJob = new ObJob();
            BeanUtils.copyProperties(paras.getObJob(),obJob);
            obJob.setCrdt(new Date());
            obJob.setCrusr($usrid);
            obJob.setStarttime(paras.getStarttime());
            obJob.setEndtime(paras.getEndtime());
            obJob.setJobid(def.getClid());
            obJobService.save(obJob);
            for(ObJobAgents x : paras.getObJobAgentsList())
            {
                x.setJobid(def.getClid());
                obJobAgentsService.save(x);
            }
        }
        return new CmdResVo(ret,def.getClid());
    }

    @Override
    @Transactional
    public CmdResVo campaignDelete(String id)
    {
        obJobService.removeById(id);
        obJobAgentsService.remove(new QueryWrapper<ObJobAgents>().eq("jobid",id));
        int ret=baseMapper.deleteById(id);
        return new CmdResVo(ret,id);
    }

    @Override
    public List<LabelResVo> campaignSelectList()
    {
        return baseMapper.campaignSelectList();
    }

    @Override
    public List<LabelResVo> dialSelectList(Integer status) {
        return null;
    }

    @Override
    public List<LabelResVo> qmSelectList(Integer status) {
        return null;
    }

    @Override
    public List<LabelResVo> getTemplateList(Integer channel) {
        return null;
    }

    @Override
    public Integer getCalllistWfID(String clid)
    {
        CpCalllist def=baseMapper.selectById(clid);
        if(def==null) return 0;
        return def.getWfid();
    }

    @Override
    public String getClid(Integer channel)
    {
        CpCalllist def=baseMapper.selectOne(new QueryWrapper<CpCalllist>()
                .eq("channel",channel)
                .last("limit 1")
        );
        if(def==null) return "";
        return def.getClid();
    }
}




